<?php
/////////////////////////////////////////////////////////////////////////////
// 
//
// workflow_system 
//
// workflow_copyright
// 
/////////////////////////////////////////////////////////////////////////////

/**
 * FILE_NAME : app_module.class.php   FILE_PATH : E:\www\\trunk\classes\application\app_module.class.php
 * ....应用 操作类
 *
 * @copyright workflow_system 
 * @author workflow Develop Team 
 * @package 
 * @subpackage 
 * @version Thu Mar 19 11:00:10 CST 2009
 */

defined('Base_Use_Sign') or die("this is invalid system!please contact <a href='http://www.shopnc.net'>www.shopnc.net</>.");

class AppModuleClass extends FrameWork {
	var $ncharset = 'utf-8';
	/**
	 * 取应用模块列表
	 * @author workflow Develop Team     
	 * @param  
	 * @return array
	 */
	function getAppModuleList($condition,&$obj_page){
		$conditionstr = $this->_conditionSearch($condition);
		$app_array = $GLOBALS['db']->GetList($conditionstr, $obj_page, "app_module");
		return $app_array;
	}
	
	/**
	 * 构造索引条件
	 * @author workflow Develop Team     
	 * @param  
	 * @return string
	 */
	function _conditionSearch($condition){
		$condition_str = '';
		
		//是否显示
		if ($condition['state'] != ''){
			$condition_str .= " and app_module_state = '" . $condition['state'] . "'";
		}
		//是否安装
		if ($condition['install'] != ''){
			$condition_str .= " and app_module_install = '" . $condition['install'] . "'";
		}
		//是否前台显示
		if ($condition['show'] != ''){
			$condition_str .= " and app_module_show = '" . $condition['show'] . "'";
		}
		//排序
		if ($condition['order_by'] != ''){
			$condition_str .= " order by ".$condition['order_by'];
		}
		
		return $condition_str;
	}	
	
	/**
	 * 取插件内容
	 * @author workflow Develop Team     
	 * @param  $id 模块ID
	 * @return array
	 */
	function getAppModuleRow($id){
		if ($id > 0){
			$app_array = $GLOBALS['db']->GetOneRow($id,'app_module','app_module_id');
			return $app_array;
		}
		return false;
	}
	
	/**
	 * 根据应用标识取应用内容
	 * @author workflow Develop Team     
	 * @param  
	 * @return int/bool/object/array/sting
	 */
	function getAppModuleRowBySign($sign){
		if (trim($sign)){
			$app_array = $GLOBALS['db']->GetOneRow($sign,'app_module','app_module_sign');
			return $app_array;
		}
		return false;
	}
	
	/**
	 * 修改应用
	 * @author workflow Develop Team     
	 * @param  $param_array 更新参数
	 * @return bool
	 */
	function updateAppModule($param_array){
		if (is_array($param_array)){
			$arr = array();
			foreach ($param_array as $k => $v){
				$arr[$k] = $v;
			}
		}
		$insert_rs = $GLOBALS['db']->UpdateRow($arr['app_module_id'],$arr, 'app_module', 'app_module_id');
		return $insert_rs;
	}
	
	/**
	 * 安装应用数据库
	 * @author workflow Develop Team     
	 * @param  $app_path 应用文件夹标识
	 * @return bool
	 */
	function setupAppModuleDatabase($app_path){
		$this->setFrameWork();
		if (strtolower($this->_configinfo['websit']['ncharset']) == 'gbk'){
			$file_name = BasePath.'/app/'.$app_path.'/install/install_gbk.sql';
		}else {
			$file_name = BasePath.'/app/'.$app_path.'/install/install_utf8.sql';
		}		
		if (file_exists($file_name)){
			$file = fopen($file_name,'rb');
			if ($file != false){
				while ($line=fgets($file)) {
					if ($line[0] != '#'){
						if (substr(trim($line),-1,1) != ';'){
							$sql .= $line;
						}else {
							$sql .= $line;
							$sql_array = explode('`',$sql);
							$sql_array[1] = NC_DBTABLEPRE.$sql_array[1];
							$sql = implode('`',$sql_array);
							$GLOBALS['db']->Execute($sql);
							unset($sql);
						}
					}
				}
				fclose($file);
				return true;
			}else {
				return false;
			}
		}else {
			return false;
		}
	}
	
	/**
	 * 调用指定应用输出的html代码
	 * @author workflow Develop Team     
	 * @param  $sign 应用标识
	 * @return string
	 */
	function getAppModuleHtmlCode($sign){
		$file_name = BasePath.'/app/'.$sign.'/classes/application/'.$sign.'.class.php';
		$app_array = $this->getAppModuleRowBySign($sign);
		if (file_exists($file_name) && $app_array['app_module_state'] == '0' && $app_array['app_module_install'] == '1'){
			require_once($file_name);
			$class_name = $sign.'Class';
			$obj_name = new $class_name();
			$html_code = $obj_name->getHtmlCode();
			return $html_code;
		}else {
			return false;
		}
	}
}
?>